﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.Windows;
using System.ServiceModel.Description;
using Service;

namespace Server
{
    public class Program
    {
        public static List<ServiceHost> services { get; set; }
        public static void Main(string[] arg)
        {
            Repository.Repository repo = new Repository.Repository();

            services = new List<ServiceHost>();

            
            StartService("ServerServices");

            Console.ReadLine();

            StopService();

        }

        private static void StartService(string ServiceName)
        {
            //1. step
            //Start services
            Uri baseAddress = new Uri("http://localhost:8000/" + ServiceName + "/");

            // Step 2 Create a ServiceHost instance
            //Type type = ServerServices;
            //Type itype = null;

            ServiceHost selfHost = new ServiceHost(typeof(ServerServices), baseAddress);

            try
            {
                // Step 3 Add a service endpoint.
                selfHost.AddServiceEndpoint(typeof(IServerServices), new WSDualHttpBinding(), ServiceName);

                // Step 4 Enable metadata exchange.
                ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true;
                selfHost.Description.Behaviors.Add(smb);

                // Step 5 Start the service.
                selfHost.Open();
                Console.WriteLine("The " + ServiceName + " service is ready.");

                services.Add(selfHost);
            }
            catch (CommunicationException ce)
            {
                Console.WriteLine("An exception occurred: {0}", ce.Message);
                selfHost.Abort();
            }
        }

        private static void StopService()
        {
            foreach (var host in services)
            {
                host.Close();
            }
        }
    }
}
